// pages/personal_data/personal_data.js
var app = getApp();

var common = require("../../common/common.js").common;

var QQMapWX = require("../../sdk/qqmap-wx-jssdk.min.js");

var qqmapsdk;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        array: [ "保密", "男", "女" ],
        region: [ "江苏省", "南通市", "崇川区" ],
        info: [],
        address_value: [],
        sex_value: "",
        chooseAddress: "",
        latitude: "",
        longitude: "",
        color_header: "",
        color_main: "",
        color_minor: "",
        birthday: "",
        jscode: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.showShareMenu({
            menus: [ "shareAppMessage", "shareTimeline" ]
        });
        common.getAppBase().then(() => {
            qqmapsdk = new QQMapWX({
                key: that.data.appBase.location_key
            });
        });
        this.getBase();
        this.getdata();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        this.login();
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    getdata: function() {
        var openid = wx.getStorageSync("user").openid;
        var that = this;
        var now = new Date();
        let str = now.getFullYear() + "-" + (now.getMonth() < 9 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1) + "-" + now.getDate();
        common.request({
            endpoint: "member/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success(res) {
                console.log(res);
                that.setData({
                    info: res.extra,
                    address_value: res.extra.address_name,
                    sex_value: res.extra.sex > 2 ? 0 : res.extra.sex,
                    chooseAddress: res.extra.area,
                    birthday: !res.extra.birthday ? str : res.extra.birthday
                });
            }
        });
    },
    login() {
        let that = this;
        wx.login({
            success(res) {
                that.setData({
                    jscode: res.code
                });
            }
        });
    },
    //选择头像
    onChooseAvatar(e) {
        let that = this;
        let domian = app.globalData.domain;
        var uniacid = wx.getStorageSync("uniacid");
        let avatar_img = e.detail.avatarUrl;
        wx.uploadFile({
            url: domian + "Lodger/lodger/product-img-upload",
            filePath: avatar_img,
            name: "file",
            formData: {},
            header: {
                "Content-Type": "multipart/form-data",
                uniacid: uniacid,
                listint: 1,
                type: 99
            },
            success: function(res) {
                var avatar_url = JSON.parse(res.data);
                that.setData({
                    ["info.img"]: avatar_url.extra.url
                });
                that.formSubmit(1);
            },
            fail: function(res) {
                wx.showModal({
                    title: "错误提示",
                    content: "上传图片失败，请重试",
                    showCancel: false,
                    success: function(res) {}
                });
            }
        });
    },
    bindPickerChange: function(e) {
        this.setData({
            sex_value: e.detail.value
        });
    },
    // 选择省市区函数
    changeRegin(e) {
        this.setData({
            address_value: e.detail.value
        });
    },
    changeNickname(e) {
        this.setData({
            ["info.nickname"]: e.detail.value
        });
        this.formSubmit(1);
    },
    changeName(e) {
        this.setData({
            ["info.name"]: e.detail.value
        });
    },
    changePhone(e) {
        this.setData({
            ["info.phone"]: e.detail.value
        });
    },
    //详细地址
    changeAddressOther(e) {
        this.setData({
            ["info.address_other"]: e.detail.value
        });
    },
    formSubmit: function(vvv) {
        const {textVal: textVal} = this.data;
        let that = this;
        let type = vvv;
        let e = {
            detail: {
                value: {
                    address_name: that.data.address_value,
                    address_other: that.data.info.address_other,
                    area: that.data.chooseAddress,
                    birthday: that.data.birthday,
                    img: that.data.info.img,
                    name: that.data.info.name,
                    nickname: that.data.info.nickname,
                    phone: that.data.info.phone,
                    sex: that.data.sex_value
                }
            }
        };
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "member/saveinfo",
            method: "post",
            data: {
                openid: openid,
                info: e.detail.value
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                wx.showToast({
                    title: "保存成功",
                    icon: "success",
                    success: res => {
                        if (type !== 1) {
                            setTimeout(() => {
                                wx.reLaunch({
                                    url: "/pages/personal/personal"
                                });
                            }, 500);
                        }
                    }
                });
            },
            fail: res => {
                wx.showToast({
                    title: "保存失败",
                    icon: "none"
                });
            }
        });
    },
    onChangeAddress: function() {
        var that = this;
        wx.chooseLocation({
            success: function(res) {
                that.setData({
                    chooseAddress: res.name,
                    latitude: res.latitude,
                    longitude: res.longitude
                });
                that.getLocal(res.latitude, res.longitude);
            },
            fail: function(err) {}
        });
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_header: res.extra.color_header,
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    communityName: res.extra.community_title ? res.extra.community_title : "社区"
                });
            }
        });
    },
    dateChange: function(e) {
        this.setData({
            birthday: e.detail.value
        });
    },
    getLocal: function(latitude, longitude) {
        let that = this;
        qqmapsdk.reverseGeocoder({
            location: {
                latitude: latitude,
                longitude: longitude
            },
            success: function(res) {
                let province = res.result.ad_info.province;
                let city = res.result.ad_info.city;
                let district = res.result.ad_info.district;
                that.setData({
                    address_value: [ province, city, district ]
                });
            },
            fail: function(res) {},
            complete: function(res) {}
        });
    },
    getPhoneNumber(e) {
        let that = this;
        var errMsg = e.detail.errMsg;
        var iv = e.detail.iv;
        var encryptedData = e.detail.encryptedData;
        common.getUserInfoNew({
            success() {
                common.request({
                    endpoint: "My/myinfo/bind-phone",
                    method: "post",
                    data: {
                        iv: iv,
                        encryptedData: encryptedData,
                        jscode: that.data.jscode,
                        center: 1
                    },
                    success: res => {
                        that.setData({
                            ["info.phone"]: res.extra.phone_number
                        });
                    },
                    fail(err) {}
                });
            }
        });
    }
});